import { Component, OnInit, EventEmitter, Output, ElementRef } from '@angular/core';

import { InitService } from 'app/public/serve/init/init.service';
import { CudTableService } from 'app/public/serve/cud-table/cud-table.service';
import { HintsService } from 'app/public/serve/hints/hints.service';

import { ipRule } from '../ip-rule.data';
import { ipRuleUrl } from '../ip-rule.url'

declare var $: any;

@Component({
  selector: 'ip-rule-edit',
  templateUrl: './ip-rule-edit.component.html',
  styleUrls: ['./ip-rule-edit.component.css']
})
export class IpRuleEditComponent implements OnInit {

  @Output() onSubmit = new EventEmitter<void>();

  details: object = {};

  constructor(
    public initService: InitService,
    private request: CudTableService,
    private hintsService: HintsService
  ) {}

  ngOnInit() {
  }

  /**
   * 详情
   * @param info 
   */
  _details(info: object) {
    this.details = info;
  }

  /**
   * 修改
   * @param info 
   */
  _onSubmit(info: object) {

    info['userId']        = ipRule.user['userId'];
    info['institutionId'] = ipRule.tree['institutionId'];

    this.request
        .postRequest(ipRuleUrl.modify, info)
        .then(res=> {
          if (res.result_code == '0000') {
            this.onSubmit.emit();
          }
          this.hintsService._operationHints(res.message, res.result_code);
          this._init();
          return
        })
        .catch(err=> {
          this.hintsService._operationHints('error', 'error');
          this._init();
          return
        })
  }

  /**
   * 初始化弹窗
   */
  private _init() {
    this.initService._init();
    $('#edit').modal('hide');
  }

}
